# 不同的操作系统，路径的分隔符是不一样的
# https://fishc.com.cn/thread-210695-1-1.html
# pathlib - 面向对象的文件相同路径

# 这是另一种导入模块的方法：表示从 pathlib 模块里面单独导入 Path
from pathlib import Path

# 获取当前目录的路径
print(Path.cwd())  # /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc

# 生成一个路径对象
p = Path("/Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc")
print(p)  # /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc
q = p / "FishC.txt"
print(q)  # /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/FishC.txt

# 判断一个路径是否为一个文件及
print(p.is_dir())  # True
print(q.is_dir())  # False

# 判断一个路径是否为一个文件
print(p.is_file())  # False
print(q.is_file())  # True

# 检测一个路径是否存在
print(p.exists())  # True
print(Path("fff").exists())  # False

# 获取路径的最后一个部分
print(p.name)  # fishc
print(q.name)  # FishC.txt

# 获取文件的文件名（stem）
print(p.stem)  # fishc
print(q.stem)  # FishC

# suffix:获取后缀
print(p.suffix)  # 空的，文件夹没有后缀
print(q.suffix)  # .txt

# parent:获取父级目录

# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc
print(p.parent)  # /Users/swordpig/Documents/codes/learn/pythohs/python_learn
# p / "FishC.txt"
print(q.parent)  # /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc

print("=" * 100)
# parents:获取逻辑祖先路径构成的不可变序列
# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc
ps = p.parents
for each in ps:
    print(each)
# /Users/swordpig/Documents/codes/learn/pythohs/python_learn
# /Users/swordpig/Documents/codes/learn/pythohs
# /Users/swordpig/Documents/codes/learn
# /Users/swordpig/Documents/codes
# /Users/swordpig/Documents
# /Users/swordpig
# /Users
# /

print("=" * 100)
# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/FishC.txt
qs = q.parents
for each in qs:
    print(each)

# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc
# /Users/swordpig/Documents/codes/learn/pythohs/python_learn
# /Users/swordpig/Documents/codes/learn/pythohs
# /Users/swordpig/Documents/codes/learn
# /Users/swordpig/Documents/codes
# /Users/swordpig/Documents
# /Users/swordpig
# /Users
# /

print("=" * 100)

print(ps[0])  # /Users/swordpig/Documents/codes/learn/pythohs/python_learn
print(ps[1])  # /Users/swordpig/Documents/codes/learn/pythohs

print("=" * 100)

# parts:将路径的各个组件拆分成元组

# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc
print(p.parts)
# ('/', 'Users', 'swordpig', 'Documents', 'codes', 'learn', 'pythohs', 'python_learn', 'fishc')

# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/FishC.txt
print(q.parts)
# ('/', 'Users', 'swordpig', 'Documents', 'codes', 'learn', 'pythohs', 'python_learn', 'fishc', 'FishC.txt')

print("=" * 100)

# stat()：查询文件或者文件夹的信息
print(p.stat())
# os.stat_result(st_mode=16877, st_ino=1717593, st_dev=16777230, st_nlink=78, st_uid=501, st_gid=20, st_size=2496, st_atime=1763370828, st_mtime=1763370826, st_ctime=1763370826)

print(q.stat())
# os.stat_result(st_mode=33188, st_ino=10839105, st_dev=16777230, st_nlink=1, st_uid=501, st_gid=20, st_size=0, st_atime=1763369209, st_mtime=1763369209, st_ctime=1763369209)

# 尺寸
print(p.stat().st_size)

# 相对路径 VS 绝对路径
# 相对路径：以当前文件夹作为基准进行一级一级推导得到的路径
# 使用 . 表示当前所在的目录
# 使用 .. 表示上一级目录
print(Path("./doc"))
print(Path("../fishc"))  # ..

# resolve():将相对路径转换为绝对路径
print(Path("./doc").resolve())  # /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc

# iterdir()：获取当前路径下所有子文件和子文件夹
d = Path("./doc").resolve()
print(d.iterdir())  # <generator object Path.iterdir at 0x1078cd270>
print("=" * 100)
for each in d.iterdir():
    print(each)

# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc/一级目录
# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc/一级文件1.txt
# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc/一级文件2.txt

# 将当前文件夹下的所有文件整理成一个列表
s = [x for x in d.iterdir() if x.is_file()]
print(type(s))
print("=" * 100)
for each in s:
    print(each)
# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc/一级文件1.txt
# /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc/一级文件2.txt


# 修改路径的操作也是支持的
# mkdir:创建目录
dir_level_1 = Path("/Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc/一级目录/")
new_dir = dir_level_1 / "FishC"
# if new_dir.exists():
#     new_dir.rmdir()
new_dir.mkdir(exist_ok=True)

new_dir = dir_level_1 / "FishC/A/B/C"
# 路径上有多个目录不存在的时候，创建目录报错
# new_dir.mkdir(exist_ok=True)  # FileNotFoundError: [Errno 2] No such file or directory: '/Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc/一级目录/FishA/B/C'
# parents 设置为 True 即可
new_dir.mkdir(exist_ok=True, parents=True)

# open()
new_file = dir_level_1 / "代码创建的文本.txt"
print(new_file)  # /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc/一级目录/代码创建的文本.txt
f = new_file.open("w")
f.write("I love my wife")
f.close()

# rename():给文件或文件夹修改名字
new_file.rename("修改名字之后的文本.txt")  # 文件的位置移动了

# replace
m = Path("修改名字之后的文本.txt")
m.replace(new_file)

# rmdir() 删除文件夹
# unlink() 删除文件
print("=" * 100)
print(new_dir)  # /Users/swordpig/Documents/codes/learn/pythohs/python_learn/fishc/doc/一级目录/FishC/A/B/C
# 将文件夹 C 删除
# new_dir.parent.rmdir()
# m.unlink()

print("=" * 100)
p = Path(".")
print(p)
# 查找
print(p.glob("*.txt"))  # <generator object Path.glob at 0x109b16350>
for each in p.glob("*.txt"):
    print(each)
    # FishC.txt

# 查询当前目录下的下一级目录中的所有*.py的文件
p = Path("/Users/swordpig/Documents/codes/learn/pythohs/python_learn")
print(list(p.glob("*/*.py")))
